//
//  ACContentView.m
//  acfun_video
//
//  Created by Q.B. on 14-3-10.
//  Copyright (c) 2014年 wang. All rights reserved.
//

#import "PageView.h"
#import "ConfigDefine.h"

@implementation PageView
{
    float _lastPostion;
    int _currentPage;
    int _pageSize;
    
    UIView *_firstView;
    UIView *_currentView;
    UIView *_lastView;
    NSTimer *_autoScrollTimer;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.scrollView.frame = self.bounds;
    self.pageControl.frame = CGRectMake(0,self.frame.size.height - 20, self.frame.size.width, 20);
    _currentPage = -1;
}

- (void) pageChange:(id)sender
{
    NSInteger page = _pageControl.currentPage;
    
    CGRect frame = _scrollView.frame;
    
    frame.origin.x = frame.size.width * page ;
    
    frame.origin.y = 0;
    
    [_scrollView scrollRectToVisible:frame animated:YES];
}

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
//    [self shouldAutoShow:NO];
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    
    [self setPageNo:_currentPage + 1];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
//    [self shouldAutoShow:YES];
    if (scrollView.contentOffset.x == _lastPostion) {
        return;
    }else if (scrollView.contentOffset.x > _lastPostion) {
        
        [self setPageNo:_currentPage + 1];
    }else if(scrollView.frame.origin.x < _lastPostion){
        
        [self setPageNo:_currentPage - 1];
    }
    
}
//白红黑蓝
-(void) setPageNo:(int) pageNo
{
    if (pageNo < 0) {
        pageNo = _pageSize;
    }
    if (pageNo > _pageSize) {
        pageNo = 0;
    }
    if (pageNo == _currentPage) {
        return;
    }
    
    int firstIndex;
    int currentIndex;
    int lastIndex;
    if (pageNo == 0) {
        firstIndex = self.pageViews.count - 1;
        currentIndex = pageNo;
        if (self.pageViews.count == 1) {
            lastIndex = 0;
        }else{
            lastIndex = 1;
        }
    }else if (pageNo == self.pageViews.count - 1){
        firstIndex = self.pageViews.count - 2;
        currentIndex = pageNo;
        lastIndex = 0;
    }else{
        
        firstIndex = pageNo-1;
        currentIndex = pageNo;
        lastIndex = pageNo+1;
    }
    [_firstView removeFromSuperview];
    [_currentView removeFromSuperview];
    [_lastView removeFromSuperview];
    
    _firstView = _pageViews[firstIndex];
    _currentView = _pageViews[currentIndex];
    _lastView = _pageViews[lastIndex];
    
    int width = _scrollView.frame.size.width;
    int height = _scrollView.frame.size.height;
    _firstView.frame = CGRectMake(0, 0, width, height);
    
    _currentView.frame = CGRectMake(width, 0, width, height);

    _lastView.frame = CGRectMake(width * 2, 0, width, height);
    if (_pageSize+1 == 1) {
        _lastView.frame = CGRectMake(width , 0, width, height);
//        _scrollView.userInteractionEnabled = NO;
        [self shouldAutoShow:NO];
    }
    [_scrollView addSubview:_firstView];
    [_scrollView addSubview:_currentView];
    [_scrollView addSubview:_lastView];
    [_scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    _lastPostion = _scrollView.frame.size.width;
    _currentPage = pageNo;
    _pageControl.currentPage = _currentPage;
}

- (void) nextPageWithAnimation:(BOOL) animation
{
    int currenWidth = _scrollView.contentOffset.x;
    int width = _scrollView.frame.size.width;
    if (currenWidth == _scrollView.contentSize.width) {
        _pageControl.currentPage = 0;
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        
        [_scrollView setContentOffset:CGPointMake(width + currenWidth, 0) animated:YES];
    }
    
    
}



-(void)autoShowNext
{
    [self nextPageWithAnimation:YES];
}

-(void)shouldAutoShow:(BOOL)autoStart
{
    if (autoStart) {
        if ([_autoScrollTimer isValid]) {
            
        }else{
           _autoScrollTimer=[NSTimer scheduledTimerWithTimeInterval:HOMEPAGEVIEWSCORLLETIME target:self selector:@selector(autoShowNext) userInfo:nil repeats:YES];
        }
    }else{
        if ([_autoScrollTimer isValid]) {
            [_autoScrollTimer invalidate];
            _autoScrollTimer = nil;
        }
    }
    
    
}
-(void)setPageViews:(NSArray *)pageViews
{
    _pageViews = pageViews;
    int count = (int)pageViews.count;
    if (count <= 0) {
        return;
    }
    _pageSize = count - 1;
    
    self.pageControl.numberOfPages = count;
    float width = self.scrollView.frame.size.width;
    float height = self.scrollView.frame.size.height;
    self.scrollView.contentSize = CGSizeMake(width * count, height);
    
    if (_currentPage == -1) {
        [self setPageNo:0];
    }else{
        [self setPageNo:_currentPage];
    }
    
//    [self shouldAutoShow:YES];
}


- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
         
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"01_04"]];
        
        _pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"01_04_2"]];
         [_pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
        _pageControl.userInteractionEnabled = NO;
        //        [self bringSubviewToFront:_pageControl];
        [self addSubview:_pageControl];
    }
    return _pageControl;
}

- (void)hiddenPageControl{
    _pageControl.hidden = YES;
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _currentPage = -1;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
